<template>
  <!-- 工程类别选择 -->
  <a-select v-bind="$attrs" v-on="$listeners" :placeholder="placeholder" v-model="currentValue"  :allowClear="isAllowClear">
    <a-spin v-if="fetching" slot="notFoundContent" size="small" />
    <a-icon v-if="fetching" style="color: #096dd9" slot="suffixIcon" type="loading" />
    <a-select-option v-for="i in list" :key="i.projectTypeId" :value="i.sourceId ? i.sourceId : i.projectTypeId">{{
      i.projectTypeName
    }}</a-select-option>
  </a-select>
</template>
<script>
import { projectType } from '@/api/quality/currency'

export default {
  name: 'SelectProjectType',
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String],
    },
    // placeholder
    placeholder: {
      type: String,
      default: () => '请选择',
    },
    // 请求参数
    id: {
      type: [String, Number],
      default: () => '',
    },
    // 是否初始化
    init: {
      type: Boolean,
      default: () => true,
    },
    // 是否显示停用工程类别
    showStop: {
      type: Boolean,
      default: () => true,
    },
    //是否默认选中第一个
    defaultCheck:{
      type: Boolean,
      default: () => false
    },
    //是否要清除
    isAllowClear:{
      type: Boolean,
      default: () => true,
    }
  },
  data() {
    return {
      fetching: false, // 加载控件
      list: [], // 列表
    }
  },
  mounted() {
    // 初始化
    this.init && this.getList()
  },
  watch: {
    // 监听参数变化
    id(e) {
      this.getList()
    },
  },
  methods: {
    /**
     * 请求列表
     */
    getList() {
      this.fetching = true
      let args = {}
      this.id && (args.id = this.id)
      projectType
        .getList(args)
        .then((res) => {
          this.fetching = false
          if (res.code == 200) {
            this.list = this.showStop ? res.data : res.data.filter(i => i.status*1 === 1)
            this.$emit('getOptions', this.$clone(res.data))
            if(this.defaultCheck && this.list.length){
              this.currentValue =  this.list[0].sourceId ? this.list[0].sourceId : this.list[0].projectTypeId;
            }
          } else {
            this.$message.error('err:', res.message)
          }
        })
        .catch((res) => {
          this.fetching = false
        })
    },
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      },
    },
  },
}
</script>
